package com.mmp.main.servlet.joborder;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.mmp.common.servlet.BasicServlet;
import com.mmp.common.util.Common;
import com.mmp.common.util.FileUtil;
import com.mmp.main.endpoints.JobOrderNotificationsEndPoint;
import com.mmp.main.entity.Employee;
import com.mmp.main.entity.HospitalDevice;
import com.mmp.main.entity.JobOrder;
import com.mmp.main.entity.LookupJobOrderPriority;
import com.mmp.main.entity.LookupJobOrderType;
import com.mmp.main.util.Default;
import com.mmp.main.util.Enums;
import com.mmp.main.util.WebUtil;

@MultipartConfig
public class JobOrderOpenProcessServlet extends BasicServlet {

	@Autowired
	JobOrderNotificationsEndPoint joNotifier;

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		int siteId = getSessionSiteId(request);

		int deviceId = WebUtil.getParamValueAsInteger(request, "deviceId", 0);

		if (deviceId == 0) {
			announceErorrMessage(request, response, Default.GENERAL_ERROR_MESSAGE);
			return;
		}

		HospitalDevice device = dbQueryManager.findById(deviceId, HospitalDevice.class);
		device.setStatus(Enums.DEVICE_STATUS.UNDER_MAINTENANCE.getStatus());

		JobOrder order = new JobOrder();
		order.setHospitalDevice(device);
		order.setJobOrderNo(WebUtil.getParamValue(request, "jobOrderNo", null));

		int type = WebUtil.getParamValueAsInteger(request, "type_id", 0);
		if (type != 0) {
			LookupJobOrderType typeObj = new LookupJobOrderType();
			typeObj.setId(type);
			order.setJobOrderType(typeObj);
		}

		int priority = WebUtil.getParamValueAsInteger(request, "priority_id", 0);
		if (priority != 0) {
			LookupJobOrderPriority priorityObj = new LookupJobOrderPriority();
			priorityObj.setId(priority);
			order.setJobOrderPriority(priorityObj);
		}

		order.setJobOrderDate(WebUtil.getParamValueAsDate(request, "jobOrderDate", null));
		order.setDamageDescription(WebUtil.getParamValue(request, "damageDescription", null));
		order.setDamageDate(WebUtil.getParamValueAsDate(request, "damageDate", null));
		order.setCancelled(false);
		order.setClosed(false);
		order.setOpenedBy(getSessionUser(request));
		order.setOpenedIn(new Date());

		String[] joRequestFormUrl = FileUtil.uploadFiles(request, "joRequestFormUrl");
		// if (joRequestFormUrl != null && joRequestFormUrl.length != 0)
		order.setRequestFormUrl(Common.concatenateValues(joRequestFormUrl));

		order.setResponsibleEngineer(dbQueryManager.findById(
				WebUtil.getParamValueAsInteger(request, "responsibleEng", 0), Employee.class));
		order.setRelatedHospitalDepartmentHead(dbQueryManager.findById(
				WebUtil.getParamValueAsInteger(request, "hospDepHead", 0), Employee.class));
		order.setMmpDepartmentHead(dbQueryManager.findById(WebUtil.getParamValueAsInteger(request, "biomedDepHead", 0),
				Employee.class));

		order.setSiteManager(dbQueryManager.getEmployees(siteId, Enums.USER_TYPE.SITE_MANGER, false).get(0));
		order.setHospitalAssistantDirector(dbQueryManager.getEmployees(siteId,
				Enums.USER_TYPE.HOSPITAL_ASSISTANT_DIRECTOR, false).get(0));
		order.setHospitalDirector(dbQueryManager.getEmployees(siteId, Enums.USER_TYPE.HOSPITAL_DIRECTOR, false).get(0));

		order.setLastCompletedPhaseId(Enums.JOBORDER_FOLLOWUP_PHASES.NOT_HANDLED_YET.getId());

		dbQueryManager.mergeEntity(order);

		// setMessage(request, new Message("تم فتح امر عمل للجهاز",
		// Message.MESSAGE_TYPE.SUCCESS));

		joNotifier.broadcastNotificationUpdates(getSessionSiteId(request));

		// sendRedirect(request, response, "OpenJobOrderSearch");

		sendRedirect(request, response, "ViewJobOrderReport?jobOrderNo=" + order.getJobOrderNo());
	}
}
